package com.ruoyi.customer.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.Contract;
import com.ruoyi.customer.domain.InWareHouse;
import com.ruoyi.customer.dto.ContractDTO;
import com.ruoyi.customer.dto.ExportRequestDTO;
import com.ruoyi.customer.mapper.ContractMapper;
import com.ruoyi.customer.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * @Author:zzm
 * @Date: 2024/9/2 10:43
 */
@Service
public class ContractServiceImpl  extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Autowired
    private ContractMapper contractMapper;

    /**
     * 合同信息获取列表
     * @param dto
     * @return
     */
    @Override
    public List<Contract> getList(ContractDTO dto) {
        List<Contract> list = contractMapper.getList(dto.getCustomerId(),dto.getCustomerNickName(),dto.getCustomerAlias(),dto.getCustomerWeight(),
                dto.getContract(),dto.getSaleName(),dto.getCustomerService());
        return list;
    }

    /**
     * 新增和修改
     * @param ro
     * @return
     */
    @Override
    public Boolean addOrUpdate(Contract ro) {
        if(ro.getId()!=null){
            this.updateById(ro);
        }else{
            this.save(ro);
        }
        return true;
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            contractMapper.deleteByIds(ids[i]);
        }
    }

    /**
     * 导出
     * @param
     */
    @Override
    public void exportByIds(HttpServletResponse response, ExportRequestDTO dto) throws JsonProcessingException {
        List<Contract> list = new ArrayList<>();
        if(dto.getIds()==null){
            list = contractMapper.getListExport();
        }else{
            list = contractMapper.getListByIdS(dto.getIds());
        }

        ObjectMapper mapper = new ObjectMapper();
        for(Contract contract :list){
            try {
                JsonNode jsonNode = mapper.readTree(contract.getPrice());

                // 使用Stream拼接name
                String result = StreamSupport.stream(jsonNode.spliterator(), false)
                        .map(node -> node.get("name").asText())
                        .collect(Collectors.joining(", "));
                contract.setPrice(result);
            } catch (Exception e) {
                // 处理解析异常，设置默认值或记录日志
                contract.setPrice("");
            }
        }
        ExcelUtil<Contract> util = new ExcelUtil<>(Contract.class);
        util.exportExcel(response, list, "合同信息");
        System.out.println("导出数据量：" + list.size());

    }
}
